package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.MenuItem;

public class GControllerScaffold extends GControllerSimple {

    Map menuMap = new HashMap();

    GControllerScaffold(String path_app, Project project, Controller controller) {
        super(path_app, project, controller);
        this.menus = project.menus;
        this.models = project.models;
        
        this.template2="javastruts2";

        this.model_scaffold = controller.model_scaffold;

        int t = menus.size();
        for (int i = 0; i < t; i++) {
            int tt = menus.get(i).menuitems.size();
            for (int ii = 0; ii < tt; ii++) {
                MenuItem menuitem = menus.get(i).menuitems.get(ii);
                menuMap.put(menuitem.controller, menuitem.caption);
            }
        }
    }

    @Override
    public void process() {
        controller();
    }

    public void controller() {
        String link = "";



        Iterator it = menuMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            String key = pairs.getKey().toString();
            if (key.equals(controller.name)) {
                link = pairs.getValue().toString().toLowerCase();
            }
        }

        String views_str = "";
        views_str += view(controller.views, controller.name);
        views_str += viewform(controller.forms, controller.name);
        views_str += viewreport(controller.reports, controller.name);

        String file = readStreamTpl("cs_controller.java.tpl");
        file = file.replace("//nameClass_plural//", controller.name);
        file = file.replace("//nameclass_plural//", controller.name.toLowerCase());
        file = file.replace("//nameClass//", this.setSingularize(controller.name));
        file = file.replace("//views_str//", views_str);
        file = file.replace("//nameclass//", this.setSingularize(controller.name).toLowerCase());
        createfile("src/java/controller/" + controller.name + "Controller.java", path, file);

        this.createDirectory(path + File.separator + "web/view/" + controller.name.toLowerCase());

        Model model = this.getModelByName(controller.model_scaffold);
        link = controller.name.toLowerCase();//+"_";

        index(controller.name, model.attributes, link);
        create(controller.name, model.attributes, this.setSingularize(controller.name).toLowerCase(), link);
        read(controller.name, this.setSingularize(controller.name), model.attributes, link);
        update(controller.name, this.setSingularize(controller.name), model.attributes, link);


    }

    public void index(String name, List<Attribute> attrs, String link) {
        int tt = attrs.size();
        String td_attrs = "";
        String th_attrs = "";
        for (int j = 0; j < tt; j++) {
            Attribute attr = attrs.get(j);
            if (attr.name.equals("name")) {
                td_attrs = td_attrs + "\t\t\t\t\t\t\t\t<td><a href=\"<s:url action=\"//link//read\" ><s:param name=\"id\" value=\"%{id}\"></s:param></s:url>\"><s:property value=\"name\" /></a></td>    \r\n";
            } else {
                td_attrs = td_attrs + "\t\t\t\t\t\t\t\t<td><s:property value=\"" + attr.name + "\" /></td> \r\n";
            }
            th_attrs = th_attrs + "<th>" + attr.name + "</th>";
        }
        String file = readStreamTpl("cs_index.jsp.tpl");
        file = file.replace("//appname//", this.appName);
        file = file.replace("//nameClass_plural//", name);
        file = file.replace("//nameclass_plural//", name.toLowerCase());
        file = file.replace("//td_attrs//", td_attrs);
        file = file.replace("//th_attrs//", th_attrs);
        file = file.replace("//link//", link);
        createfile("web/view/" + name.toLowerCase() + "/index.jsp", path, file);
    }

    public void create(String name, List<Attribute> attrs, String name_singularize, String link) {
        int tt = attrs.size();
        String textfield_attrs = "";
        for (int j = 0; j < tt; j++) {
            Attribute attr = attrs.get(j);
            if (!attr.name.equals("id")) {
                textfield_attrs = textfield_attrs + "\t\t\t<s:textfield label=\"" + attr.name + "\" name=\"" + name_singularize + "." + attr.name + "\" /> \r\n";

            }

        }
        String file = readStreamTpl("cs_create.jsp.tpl");
        file = file.replace("//appname//", this.appName);
        file = file.replace("//nameClass//", name);
        file = file.replace("//nameclass//", name.toLowerCase());
        file = file.replace("//textfield_attrs//", textfield_attrs);
        file = file.replace("//link//", link);
        createfile("web/view/" + name.toLowerCase() + "/create.jsp", path, file);
    }

    public void read(String name, String name_singularize, List<Attribute> attrs, String link) {
        int tt = attrs.size();
        String textfield_attrs = "";
        for (int j = 0; j < tt; j++) {
            Attribute attr = attrs.get(j);
            String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());
            textfield_attrs = textfield_attrs + "\t\t\t\t<s:textfield label=\"" + attr.name + "\" name=\"" + name_singularize.toLowerCase() + "." + attr.name + "\"   value=\"%{" + name_singularize.toLowerCase() + ".get" + NameAtrr + "()}\" disabled=\"true\" /> \r\n";
            //<s:textfield label="Name" name="article.name" size="50" value="%{article.getName()}" disabled="true" />
        }
        String file = readStreamTpl("cs_read.jsp.tpl");
        file = file.replace("//appname//", this.appName);
        file = file.replace("//nameClass//", name);
        file = file.replace("//nameclass//", name.toLowerCase());
        file = file.replace("//label_attrs//", textfield_attrs);
        file = file.replace("//link//", link);
        createfile("web/view/" + name.toLowerCase() + "/read.jsp", path, file);
    }

    public void update(String name, String name_singularize, List<Attribute> attrs, String link) {
        int tt = attrs.size();
        String textfield_attrs = "";
        for (int j = 0; j < tt; j++) {
            Attribute attr = attrs.get(j);
            String NameAtrr = attr.name.substring(0, 1).toUpperCase() + attr.name.substring(1, attr.name.length());
            textfield_attrs = textfield_attrs + "\t\t\t\t<s:textfield label=\"" + NameAtrr + "\" name=\"" + name_singularize.toLowerCase() + "." + attr.name + "\" value=\"%{" + name_singularize.toLowerCase() + ".get" + NameAtrr + "()}\" /> \r\n";
            //<s:textfield label="Name" name="article.name" size="50" value="%{article.getName()}" />      
        }
        String file = readStreamTpl("cs_update.jsp.tpl");
        file = file.replace("//appname//", this.appName);
        file = file.replace("//nameClass//", name);
        file = file.replace("//nameclass//", name.toLowerCase());
        file = file.replace("//name_singularize//", name_singularize.toLowerCase());
        file = file.replace("//textfield_attrs//", textfield_attrs);
        file = file.replace("//link//", link);
        createfile("web/view/" + name.toLowerCase() + "/update.jsp", path, file);
    }
}
